/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fusiondata;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DataFromDB {

    protected  Connection conn = null;


    public ResultSet getResultSet(String sql) {
        Statement sm;
        try {
            sm = conn.createStatement();
            return sm.executeQuery(sql);
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public ResultSet getStructure() {
        try {
            DatabaseMetaData meta = conn.getMetaData();
            return meta.getTables(null, null, null,new String[] { "TABLE" });
        } catch (SQLException ex) {
            Logger.getLogger(UseAccess.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public ResultSet getColumns(String tableName){
        try {
            DatabaseMetaData meta = conn.getMetaData();
//            return meta.getColumns(null, "%", tableName, "%");
            return meta.getColumns(null, null, tableName, "%");
        } catch (SQLException ex) {
            Logger.getLogger(UseAccess.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void close() {
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(UseAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isConnected() {
        return conn != null;
    }
}
